from Data import Data
from MetaData import MetaDataFactory,MetaData
import activist.Time
from activist import errors

ACCESSIBLE_KEYS = ['smoking', 'wheelchair', 'pets', 'signlanguage', 'childcare']
KEY_TO_DESCRIPTION = {
    'smoking': 'Smoking Permitted',
    'wheelchair': 'Wheelchair access',
    'pets': 'Pets allowed',
    'signlanguage': 'Sign language available.',
    'childcare': 'Childcare is available.'
    }
KEY_TO_ANTIDESCRIPTION = {
    'smoking': 'No Smoking',
    'wheelchair': 'No Wheelchair Access',
    'pets': 'No Pets',
    'signlanguage': 'No sign language interpretation.',
    'childcare': 'No childcare available.'
    }

KEY_TO_IMAGE = {
    'smoking': '/noproxy/smoking.png',
    'wheelchair': '/noproxy/wheelchair.png',
    'pets': '/noproxy/pets.png',
    'signlanguage': '/noproxy/signlanguage.png',
    'childcare': '/noproxy/childcare.png'
    }
KEY_TO_ANTIIMAGE = {
    'smoking': '/noproxy/no-smoking.png',
    'wheelchair': '/noproxy/no-wheelchair.png',
    'pets': '/noproxy/no-pets.png',
    'signlanguage': '/noproxy/no-signlanguage.png',
    'childcare': '/noproxy/no-childcare.png'
    }

## from grok-listings script
TYPES = ['movie', 'vigil', 'meeting', 'presentation', 'conference', 'potluck', 'openhouse', 'workshop', 'lecture', 'reading', 'protest', 'performance', 'panel', 'dinner', 'picket', 'festival', 'memorial', 'party', 'concert', 'other', 'training', 'fundraiser', 'specialday', 'showing', 'march', 'radio', 'benefit', 'camp', 'dance']
TYPES.sort()

class EventInformation(Data):

    def __init__(self,event,title,when,access,description,brief,place,listingtype,contacts,creator=None):
        ## do not serialize creation (other commands do this; event.create, event.edit)
        
        if not isinstance(when,activist.Time.Time):
            raise "when must be an activist.Time.Time object"
        if not listingtype in TYPES:
            raise "invalid listing type " + str(listingtype)
        if not isinstance(contacts,list):
            raise "contacts must be a list"
        self._when = when               # any Time object
        self._description = description
        self._brief = brief
        self._title = title
        self._type = listingtype
        self._access = []
        self._contacts = contacts
        for x in self._contacts:
            if not isinstance(x, activist.Person.Person):
                raise "All contacts must be people"
        if access:
            for k in access:
                if k in ACCESSIBLE_KEYS:
                    self._access.append(k)
                else:
                    raise errors.IllegalArgument(str(k)+" is not a valid accessible key.")
        
        self.id()
        super(EventInformation,self).__init__()

        if event:
            meta = MetaDataFactory(self,MetaData.InformationOf,event)
            if creator:
                MetaDataFactory(meta,MetaData.Creator,creator)
        if place:
            MetaDataFactory(self,MetaData.LocatedAt,place)

                                        # debug for Data, but we don't have a create()
        self._createcalled = True


    def smoking(self):
        return 'smoking' in self._access

    def wheelchair(self):
        return 'wheelchair' in self._access

    def pets(self):
        return 'pets' in self._access

    def childcare(self):
        return 'childcare' in self._access

    def signlanguage(self):
        return 'signlanguage' in self._access

    def contacts(self):
        return self._contacts
    
    def title(self):
        return self._title

    def when(self):
        return self._when.format()

    def whenRaw(self):
        return self._when

    def event(self):
        for x in self.metadataTargetsForRelationship(MetaData.InformationOf):
            return x.toData()
        return None

    def where(self):
        try:
            return self.metadataTargetsForRelationship(MetaData.LocatedAt)[0].toData()
        except IndexError:
            return None

    def description(self):
        return self._description

    def brief(self):
        return self._brief

    def getXML(self):
        return """
<eventinformation class="activist.EventInformation" id="%(id)s">
  <title>%(title)s</title>
  <when>%(when)s</when>
  <description>%(description)s</description>
%(meta)s
</eventinformation>
        """ % { "when": self._when,
                "description": self._description,
                "id":self.id(),
                "title":self.title(),
                "meta":self.getMetaXML()
                }
